package LeetCode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class No39 {
    public static void main(String[] args) {
        combinationSum(new int[]{2,3,5},8);
    }
    public static List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> list = new ArrayList<>();
        Arrays.sort(candidates);
        backTrack(list,candidates,target,0,0,new ArrayList<>());
        System.out.println(list);
        return list;
    }
    public static void backTrack(List<List<Integer>> list,int[] a,int target,int index,int sum,List<Integer> list1){
        if(sum>target) return;
        if(sum==target&&!list.contains(list1)) {
            list.add(new ArrayList<>(list1));
            return;
        }
        for (int i = index; i < a.length; i++) {
            sum+=a[i];
            list1.add(a[i]);
            backTrack(list,a,target,i,sum,list1);
            list1.remove(list1.size()-1);
            sum-=a[i];
        }
    }
}
